home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE11 / GLOBALOC.C < prev    next >
C/C++ Source or Header  |  1996-01-29  |  8KB  |  263 lines

  1.  
  2. #include <windows.h>  
  3. #include "GlobAloc.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static PAINTSTRUCT ps;
  110. static char        szBuf[128];
  111. static LPTSTR      pMem = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               {
  117.                  // Allocate Initial block of memory and
  118.                  // Initialize it with the alphabet in uppercase.
  119.                  //..............................................
  120.                  HGLOBAL hMem = GlobalAlloc( GHND, 27 );
  121.                  LPTSTR  pCur;
  122.  
  123.                  if ( hMem && (pMem = (LPTSTR)GlobalLock( hMem )) != NULL )
  124.                  {
  125.                     int i;
  126.  
  127.                     pCur = pMem;
  128.                     for( i=0; i<26; i++ )
  129.                         *pCur++ = 'A'+i;
  130.  
  131.                     *pCur = 0;
  132.                  }
  133.                  else
  134.                  {
  135.                     MessageBox( hWnd, "Could not allocate memory", 
  136.                                 "Memory Error", 
  137.                                 MB_OK | MB_ICONSTOP );
  138.  
  139.                     DestroyWindow( hWnd );
  140.                  }
  141.               }
  142.               break;
  143.  
  144.       case WM_PAINT :
  145.               // Paint the status of the memory.
  146.               //................................
  147.               BeginPaint( hWnd, &ps );
  148.               if( pMem )
  149.               {
  150.                  // Retrieve the handle for
  151.                  // the memory and memory status.
  152.                  //..............................
  153.                  HGLOBAL hMem   = GlobalHandle( pMem );
  154.                  DWORD   dwSize = GlobalSize( hMem );
  155.                  UINT    uFlags = GlobalFlags( hMem );
  156.  
  157.                  TextOut( ps.hdc, 10, 10, szBuf, wsprintf( szBuf,
  158.                          "The memory block is %ld bytes in size.", dwSize ) );
  159.                  TextOut( ps.hdc, 10, 30, szBuf, wsprintf( szBuf, 
  160.                           "Contains: %s", pMem ) );
  161.                  TextOut( ps.hdc, 10, 50, szBuf, wsprintf( szBuf,
  162.                           "Flags: Discardable=%d, Lock Count=%d",
  163.                           uFlags & GMEM_DISCARDABLE, 
  164.                           uFlags & GMEM_LOCKCOUNT ) );
  165.               }
  166.               EndPaint( hWnd, &ps );
  167.               break;
  168.  
  169.       case WM_COMMAND :
  170.               switch( LOWORD( wParam ) )
  171.               {
  172.                  case IDM_TEST :
  173.                         // ReAllocate memory and add the 
  174.                         // lowercase alphabet to the memory.
  175.                         //..................................
  176.                         if ( pMem )
  177.                         {
  178.                            HGLOBAL hMem = GlobalHandle( pMem );
  179.  
  180.                            GlobalUnlock( hMem );
  181.                            pMem = NULL;
  182.  
  183.                            hMem = GlobalReAlloc( hMem, (26*2)+1, 
  184.                                                  GMEM_MOVEABLE );
  185.                            if ( hMem )
  186.                            {
  187.                               LPTSTR pCur;
  188.                               int    i;
  189.  
  190.                               pMem = (LPTSTR)GlobalLock( hMem );
  191.  
  192.                               // Skip the old values.
  193.                               //.....................
  194.                               pCur = pMem+26;                  
  195.  
  196.                               // Set the new values.
  197.                               //....................
  198.                               for( i=0; i<26; i++ )
  199.                                  *pCur++ = 'a'+i;
  200.  
  201.                               *pCur = 0;
  202.                            }
  203.                            else
  204.                               MessageBox( hWnd, "Could not allocate memory",
  205.                                          "Memory Error", 
  206.                                           MB_OK | MB_ICONSTOP );
  207.  
  208.                            InvalidateRect( hWnd, NULL, TRUE );
  209.                         }
  210.                         break;
  211.  
  212.                  case IDM_ABOUT :
  213.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  214.                         break;
  215.  
  216.                  case IDM_EXIT :
  217.                         DestroyWindow( hWnd );
  218.                         break;
  219.               }
  220.               break;
  221.       
  222.       case WM_DESTROY :
  223.               if ( pMem )
  224.               {
  225.                  HGLOBAL hMem = GlobalHandle( pMem );
  226.  
  227.                  GlobalUnlock( hMem );
  228.                  GlobalFree( hMem );
  229.               }
  230.               PostQuitMessage(0);
  231.               break;
  232.  
  233.       default :
  234.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  235.    }
  236.  
  237.    return( 0L );
  238. }
  239.  
  240.  
  241. LRESULT CALLBACK About( HWND hDlg,           
  242.                         UINT message,        
  243.                         WPARAM wParam,       
  244.                         LPARAM lParam)
  245. {
  246.    switch (message) 
  247.    {
  248.        case WM_INITDIALOG: 
  249.                return (TRUE);
  250.  
  251.        case WM_COMMAND:                              
  252.                if (   LOWORD(wParam) == IDOK         
  253.                    || LOWORD(wParam) == IDCANCEL)    
  254.                {
  255.                        EndDialog(hDlg, TRUE);        
  256.                        return (TRUE);
  257.                }
  258.                break;
  259.    }
  260.  
  261.    return (FALSE); 
  262. }
  263.